package gd.qaq.blue.boundary.listener;

import gd.qaq.blue.boundary.Boundary;
import gd.qaq.blue.boundary.dto.Point;
import gd.qaq.blue.boundary.enums.BorderState;
import gd.qaq.blue.boundary.enums.PointType;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;

import java.util.Map;

import static gd.qaq.blue.boundary.enums.BorderState.TP;
import static gd.qaq.blue.boundary.enums.PointType.*;
import static gd.qaq.blue.boundary.util.PluginHelper.error;

@SuppressWarnings("all")
public class BorderListener implements Listener {
    @EventHandler
    public void onMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        String worldName = player.getWorld().getName();
        if (!Boundary.borderMappers.containsKey(worldName)) {
            return;
        }
        Map<PointType, Point> pointMapper = Boundary.borderMappers.get(worldName);
        Location location = player.getLocation();
        double y = location.getY();
        if (y >= pointMapper.get(YMAX).getValue()) {
            teleportPlayer(player, pointMapper, YMAX, pointMapper.get(YMAX).getState());
            return;
        } else if (y <= pointMapper.get(YMIN).getValue()) {
            teleportPlayer(player, pointMapper, YMIN, pointMapper.get(YMIN).getState());
            return;
        }
        double z = location.getZ();
        if (z >= pointMapper.get(ZMAX).getValue()) {
            teleportPlayer(player, pointMapper, ZMAX, pointMapper.get(ZMAX).getState());
            return;
        } else if (z <= pointMapper.get(ZMIN).getValue()) {
            teleportPlayer(player, pointMapper, ZMIN, pointMapper.get(ZMIN).getState());
            return;
        }
        double x = location.getX();
        if (x >= pointMapper.get(XMAX).getValue()) {
            teleportPlayer(player, pointMapper, XMAX, pointMapper.get(XMAX).getState());
            return;
        } else if (x <= pointMapper.get(XMIN).getValue()) {
            teleportPlayer(player, pointMapper, XMIN, pointMapper.get(XMIN).getState());
            return;
        }
    }


    private void teleportPlayer(Player player, Map<PointType, Point> pointMapper, PointType type, BorderState state) {
        Location location = player.getLocation();
        switch (type) {
            case YMAX:
                location.setY(state == TP ? (pointMapper.get(YMIN).getValue() + 0.5d) : (pointMapper.get(YMAX).getValue() - 2.0d));
                break;
            case YMIN:
                location.setY(state == TP ? (pointMapper.get(YMAX).getValue() - 0.5d) : (pointMapper.get(YMIN).getValue() + 2.0d));
                break;
            case ZMAX:
                location.setZ(state == TP ? (pointMapper.get(ZMIN).getValue() + 0.5d) : (pointMapper.get(ZMAX).getValue() - 2.0d));
                break;
            case ZMIN:
                location.setZ(state == TP ? (pointMapper.get(ZMAX).getValue() - 0.5d) : (pointMapper.get(ZMIN).getValue() + 2.0d));
                break;
            case XMAX:
                location.setX(state == TP ? (pointMapper.get(XMIN).getValue() + 0.5d) : (pointMapper.get(XMAX).getValue() - 2.0d));
                break;
            case XMIN:
                location.setX(state == TP ? (pointMapper.get(XMAX).getValue() - 0.5d) : (pointMapper.get(XMIN).getValue() + 2.0d));
        }
        error(player, "你已到达" + type.toString().substring(0, 1) + "轴边界!");
        player.teleport(location);
    }
}
